/* Copyright (c) <2003-2016> <Newton Game Dynamics>
* 
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
* 
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely
*/  
   
#version 330 core
uniform mat4 projectionMatrix;
uniform vec4 spriteRadius;

//uniform vec4 directionalLightDir;

in vec4 quadUV;
in vec4 quadColor;
in vec4 spriteOrigin;

out vec4 pixelColor;

void main()
{
	float r2 = dot(quadUV, quadUV);
	if (r2 > 1.0) 
	{	
		discard; 
	}

	vec4 normalInPixelSpace = vec4(quadUV.x, quadUV.y, sqrt (1.0 - r2), 0.0f);

	// here I need to rest the z buffer and prevent z fitgting
	vec4 pointOnSphere = spriteOrigin + normalInPixelSpace * spriteRadius;
	vec4 newPointOnSphere = projectionMatrix * pointOnSphere;
	gl_FragDepth = newPointOnSphere.z / newPointOnSphere.w;

	// TODO
	// now I need the light vector also in pixel space
	// which should be an interpolated input, 
	// for now just hack a fix value
	vec4 directionalLightDir = vec4(0.577, 0.577, 0.577, 0.0);
	float difusse = max (dot(normalInPixelSpace, directionalLightDir), 0.3) ;

	pixelColor = quadColor * difusse;
}